<?php
/**
 * 线索
 * @Author: wjf
 * @Date: 2022-12-01 09:14:08
 */

namespace app\crm\controller\clue;

use app\crm\model\client\Ctrecord;
use app\crm\model\client\Ourclients;
use app\crm\model\clue\Follow as ClueFollow;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\facade\Session;

class Follow extends Controller
{
    /**
     * 主页面
     * @Author: wjf
     * @Date: 2022-12-01 09:16:16
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 遍历跟进数据
     * @Author: wjf
     * @Date: 2022-12-02 16:12:45
     */
    public function list()
    {
        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $data = ClueFollow::limit($page, $_GET['limit'])->where('classify_id', Session::get('f_id'))->where('classify', 3)->select();
        $count = ClueFollow::all()->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }


    /**
     * 跟进 
     * @Author: wjf
     * @Date: 2022-12-01 11:38:24
     */
    public function followup()
    {
        if (!empty($_GET['id'])) {
            Session::set('f_id', $_GET['id']);
            $list = db('crm_client')->where('id', $_GET['id'])->select();
            $this->assign(['list' => $list]);
        }
        return $this->fetch();
    }

    /**
     * 跟进表添加数据
     * @Author: wjf
     * @Date: 2022-12-01 11:38:24
     */
    public function fpdata()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $data['classify_id'] = Session::get('f_id');
            $ClueFollow = new ClueFollow;
            if ($ClueFollow->save($data)) {
                return json(['code' => 1, 'msg' => '跟进成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟进失败']);
            }
        }
    }

    /**
     * 跟进表添加数据
     * @Author: wjf
     * @Date: 2022-12-01 11:38:24
     */
    public function fpdatad()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $data = $this->request->post();
            $data['classify_id'] = Session::get('f_id');
            $Ctrecord = new Ctrecord();
            if ($Ctrecord->save($data)) {
                return json(['code' => 1, 'msg' => '跟进成功']);
            } else {
                return json(['code' => 0, 'msg' => '跟进失败']);
            }
        }
    }

    /**
     * 遍历数据 
     * @Author: wjf
     * @Date: 2022-12-02 19:33:35
     */
    public function indata()
    {
        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $data = Ourclients::limit($page, $_GET['limit'])->where('status', 3)->where('super_id', 0)->where('classify', 2)->select();
        $count = Ourclients::where('super_id', 0)->where('classify', 2)->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }

    /**
     * 搜索查询
     * @Author: wjf
     * @Date: 2022-12-03 09:19:50
     */
    public function lists()
    {
        $aa = $this->request->get('data');
        $arr = [];
        foreach ($aa as $k => $v) {
            if (!$v) {
                unset($aa[$k]);
            } else {
                $arr[$k] = $v;
            }
        }
        $page = ($_GET['page'] - 1) * $_GET['limit'];
        $Ourclients = new Ourclients();
        $data = $Ourclients->limit($page, $_GET['limit'])->where($arr)->where('super_id', 0)->where('status', 3)->where('classify', 2)->select();
        $count = $Ourclients->where($arr)->where('super_id', 0)->where('status', 3)->where('classify', 2)->count();
        return json(['code' => 0, 'msg' => '遍历', 'count' => $count, 'data' => $data]);
    }

    /**
     * 领取 
     * @Author: wjf
     * @Date: 2022-12-03 18:44:27
     */
    public function get()
    {
        if ($this->request->isGet() && $this->request->isAjax()) {
            $time = time();
            if (Ourclients::where('id', $_GET['id'])->update(['super_id' => 1, 'status' => 3, 'classify' => 2,'get_time' => $time])) {
                return json(['code' => 0, 'msg' => '领取成功']);
            }
        }
    }

    /**
     * 批量领取 
     * @Author: wjf
     * @Date: 2022-12-03 18:44:27
     */
    public function getd()
    {
        // 启动事务
        Db::startTrans();
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $_POST['id'];
            $time = time();
            $arr = explode(',', $id);
            array_pop($arr);
            $list = [];
            try {
                foreach ($arr as $k => $v) {
                    Ourclients::where('id', $v)->update(['super_id' => 1, 'status' => 3, 'classify' => 2,'get_time' => $time]);
                }
                // 提交事务
                Db::commit();
                return json(['code' => 0, 'msg' => '领取成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 1, 'msg' => '领取失败']);
            } 
        }
    }
}
